package com.abe.libcore.base.screen

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.abe.libquick.utils.NormalUtils
import com.abe.libquick.utils.SpQuickHelper
import java.util.*

abstract class AbeActivity : BScreenActivity() {
    private val appToast: Int = 0x999

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutRes())
        initContent()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        resources
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res = super.getResources()
        val newConfig = res.configuration
        newConfig.fontScale = SpQuickHelper.fontScale() //设置标准
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            return createConfigurationContext(newConfig).resources;
        } else {
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }

    protected abstract fun getLayoutRes(): Int

    protected abstract fun initContent()

    /**********************************************************************************************/
    protected fun getContext(): Context {
        return this
    }

    protected fun gotoActivity(act: Class<*>) {
        startActivity(getIntent(act))
    }

    protected fun gotoActivity(act: Class<*>, extras: Bundle) {
        val intent = getIntent(act)
        intent.putExtras(extras)
        startActivity(intent)
    }

    protected fun gotoActivityForResult(act: Class<*>, code: Int) {
        startActivityForResult(getIntent(act), code)
    }

    protected fun gotoActivityForResult(act: Class<*>, extras: Bundle, code: Int) {
        val intent = getIntent(act)
        intent.putExtras(extras)
        startActivityForResult(intent, code)
    }

    protected fun setResultOK(bundle: Bundle) {
        val intent = Intent()
        intent.putExtras(bundle)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    protected fun setResultCancel() {
        setResult(Activity.RESULT_CANCELED)
        finish()
    }

    protected open fun bundle(): Bundle? {
        return intent.extras
    }

    private fun getIntent(act: Class<*>): Intent {
        return Intent(this@AbeActivity, act)
    }

    /** */
    open fun getEmpty(value: String?): String? {
        return if (TextUtils.isEmpty(value)) "" else value
    }

    open fun getText(editText: EditText?): String? {
        return editText?.text?.toString()?.trim { it <= ' ' } ?: ""
    }

    open fun getText(textView: TextView?): String? {
        return textView?.text?.toString()?.trim { it <= ' ' } ?: ""
    }

    open fun stringToDate(dateStr: String?, rule: String?): Date? {
        return NormalUtils.stringToDate(rule, dateStr)
    }

    open fun dateToString(date: Date?, rule: String?): String? {
        return NormalUtils.dateToString(rule, date)
    }

    /**********************************************************************************************/
    inner class BaseHandler(looper: Looper) : Handler(looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                appToast -> Toast.makeText(
                    getContext(),
                    msg.obj.toString(),
                    Toast.LENGTH_SHORT
                ).show()

                else -> if (isImp(this@AbeActivity.javaClass, OnHandlerListener::class.java)) {
                    (this@AbeActivity as OnHandlerListener).dispatchHandleMessage(msg)
                }
            }
        }
    }

    protected fun toast(content: String) {
        if (TextUtils.isEmpty(content)) return
        localSendMsg(appToast, content)
    }

    protected open fun toast(strRes: Int) {
        toast(resources!!.getString(strRes))
    }

    protected open fun toast(rule: String?, vararg objects: Any?) {
        toast(String.format(Locale.CHINA, rule!!, *objects))
    }

    protected fun localSendMsg(what: Int, obj: Any) {
        getHandler().sendMessage(getMessage(what, obj))
    }

    protected open fun localEmptyMsg(what: Int) {
        getHandler().sendEmptyMessage(what)
    }

    //获取Message
    protected fun getMessage(what: Int, obj: Any): Message {
        val msg = Message()
        msg.what = what
        msg.obj = obj
        return msg
    }

    //获取Handler
    protected fun getHandler(): BaseHandler {
        return BaseHandler(Looper.getMainLooper())
    }

    /**
     * 是否实现接口
     * @param a 类
     * @param b 接口
     */
    protected fun isImp(a: Class<*>, b: Class<*>): Boolean {
        return b.isAssignableFrom(a)
    }
}